package com.siburian.MyStuff;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class Tes_Pelajaran {
	private static final String ID_TEST = "ID_Tes";
	private static final String ID_BAB = "ID_Bab";
	private static final String ID_PELAJARAN = "ID_Pelajaran";
	private static final String ID_KELAS = "ID_Kelas";
	private static final String CONTENT = "Content";
	private static final String OPSI1 = "Opsi1";
	private static final String OPSI2 = "Opsi2";
	private static final String OPSI3 = "Opsi3";
	private static final String OPSI4 = "Opsi4";
	private static final String JAWABANTEST = "JawabanTest";
	 
	private static final String NAMA_DB = "DBTestAnak";// nama database
	private static final String NAMA_TABEL = "TestKemampuan";// nama tabel
	private static final int DB_VERSION = 1;
 
	private static final String CREATE_TABLE_PROFILE = "create table "+NAMA_TABEL+" ("+ID_TEST+" integer PRIMARY KEY autoincrement,"+ID_BAB+" integer, "+ID_KELAS+" integer,"+ID_PELAJARAN+" integer,"+CONTENT+" BLOB,"+OPSI1+" text,"+OPSI2+" text,"+OPSI3+" text,"+OPSI4+" text,"+JAWABANTEST+" integer)";
	private final Context context;
	private DatabaseOpenHelper dbHelper;
	private SQLiteDatabase db;
 
	public Tes_Pelajaran(Context ctx) {
		this.context = ctx;
		dbHelper = new DatabaseOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}
 
	private static class DatabaseOpenHelper extends SQLiteOpenHelper {
		 
		public DatabaseOpenHelper(Context context) {
			super(context, NAMA_DB, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_TABLE_PROFILE);
		}
 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+NAMA_TABEL);
			onCreate(db);
 
		}
	}
 
	public void close() {
		dbHelper.close();
	}
 
	public void addProfile(int id_bab,int id_kelas,int id_pelajaran,String content, String opsi1, String opsi2, String opsi3, String opsi4,int jawaban) {
 
		ContentValues values = new ContentValues();
		values.put(ID_BAB, id_bab);
		values.put(ID_KELAS, id_kelas);
		values.put(ID_PELAJARAN, id_pelajaran); 
		values.put(CONTENT, content);
		values.put(OPSI1, opsi1);
		values.put(OPSI2, opsi2);
		values.put(OPSI3, opsi3);
		values.put(OPSI4, opsi4);
		values.put(JAWABANTEST, jawaban);
		
		try {
 
		//	db.delete(NAMA_TABEL, null, null);
			db.insert(NAMA_TABEL, null, values);
		} catch (Exception e) {
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}
	}
	
	
	public ArrayList<ArrayList<Object>> getSoalTest(int idBab, int idKelas, int idPelajaran,int batas){
		ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] {ID_TEST,OPSI1,OPSI2,OPSI3,OPSI4,JAWABANTEST }, ID_BAB + "=" + '"' + idBab + '"'+ " and " + ID_KELAS+ "=" + '"' + idKelas + '"'+ " and " + ID_PELAJARAN+ "=" + '"' + idPelajaran + '"', null,
					"RANDOM() LIMIT " + batas, null, null);
			cur.moveToFirst();
			if (!cur.isAfterLast()) {
				do {
					ArrayList<Object> dataList = new ArrayList<Object>();
					dataList.add(cur.getInt(0));
					dataList.add(cur.getString(1));
					dataList.add(cur.getString(2));
					dataList.add(cur.getString(3));
					dataList.add(cur.getString(4));
					dataList.add(cur.getInt(5));
					dataArray.add(dataList);
 
				} while (cur.moveToNext());
 
			}
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return dataArray;
	}
	
	public byte[] getImageFromDatabase(int idUrutan){
		byte[] image = null;
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] {ID_TEST, CONTENT },  ID_TEST + "=" + '"' + idUrutan + '"', null,
					null, null, null);
			cur.moveToFirst();
            if(cur.moveToFirst()) {
                image = cur.getBlob(cur.getColumnIndexOrThrow(CONTENT));
        }
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return image;
	}
	public void generateDataTest(byte[] image1,byte[] image2,byte[] image3,byte[] image4,byte[] image5){
		ContentValues values = new ContentValues();
		values.put(ID_BAB, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(CONTENT, image1); 
		values.put(OPSI1, "Apple"); 
		values.put(OPSI2, "Aple"); 
		values.put(OPSI3, "Alpe"); 
		values.put(OPSI4, "Apel");
		values.put(JAWABANTEST, 1);
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ID_BAB, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(CONTENT, image2); 
		values.put(OPSI1, "Mangoe"); 
		values.put(OPSI2, "Mango"); 
		values.put(OPSI3, "Mangga"); 
		values.put(OPSI4, "Manga");
		values.put(JAWABANTEST, 2);
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ID_BAB, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(CONTENT, image3); 
		values.put(OPSI1, "Bananan"); 
		values.put(OPSI2, "Bannana"); 
		values.put(OPSI3, "Banana"); 
		values.put(OPSI4, "Banan");
		values.put(JAWABANTEST, 3);
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ID_BAB, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(CONTENT, image4); 
		values.put(OPSI1, "Grap"); 
		values.put(OPSI2, "Grapel"); 
		values.put(OPSI3, "Grapes"); 
		values.put(OPSI4, "Grape");
		values.put(JAWABANTEST, 1);
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ID_BAB, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(CONTENT, image5); 
		values.put(OPSI1, "Pineaple"); 
		values.put(OPSI2, "Pineaples"); 
		values.put(OPSI3, "Pinneaple"); 
		values.put(OPSI4, "Pineaplel");
		values.put(JAWABANTEST, 1);
		db.insert(NAMA_TABEL, null, values);
	}


}
